home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / src / subshell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  31.2 KB  |  1,227 lines

  1. /* {{{ Copyright notice */
  2.  
  3. /* Concurrent shell support for the Midnight Commander
  4.    Copyright (C) 1994, 1995 Dugan Porter
  5.  
  6.    This program is free software; you can redistribute it and/or
  7.    modify it under the terms of Version 2 of the GNU General Public
  8.    License, as published by the Free Software Foundation.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* }}} */
  21.  
  22. #include <config.h>
  23. #ifdef HAVE_SUBSHELL_SUPPORT
  24.  
  25. /* {{{ Declarations */
  26.  
  27. #include <stdio.h>      
  28. #include <stdlib.h>    /* For errno, putenv, etc.          */
  29. #include <errno.h>    /* For errno on SunOS systems          */
  30. #include <termios.h>    /* tcgetattr(), struct termios, etc.  */
  31. #include <sys/types.h>    /* Required by unistd.h below          */
  32. #include <sys/ioctl.h>    /* For ioctl() (surprise, surprise)   */
  33. #include <fcntl.h>    /* For open(), etc.              */
  34. #include <string.h>    /* strstr(), strcpy(), etc.          */
  35. #include <signal.h>    /* sigaction(), sigprocmask(), etc.   */
  36. #include <sys/time.h>    /* select(), gettimeofday(), etc.     */
  37. #include <sys/stat.h>    /* Required by dir.h & panel.h below  */
  38. #include <sys/param.h>    /* Required by panel.h below          */
  39. #include "tty.h"
  40.  
  41. #ifdef HAVE_UNISTD_H
  42. #   include <unistd.h>    /* For pipe, fork, setsid, access etc */
  43. #endif
  44.  
  45. #ifdef IS_AIX
  46. #   include <sys/select.h>
  47. #endif
  48.  
  49. #ifdef HAVE_SYS_WAIT_H
  50. #   include <sys/wait.h> /* For waitpid() */
  51. #endif
  52.  
  53. #ifndef WEXITSTATUS
  54. #   define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  55. #endif
  56.  
  57. #ifndef WIFEXITED
  58. #   define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  59. #endif
  60.  
  61. #ifdef HAVE_GRANTPT
  62. #   include <stropts.h> /* For I_PUSH                  */
  63. #else
  64. #   include <grp.h>    /* For the group struct & getgrnam()  */
  65. #endif
  66.  
  67. #include "dir.h"    /* Required by panel.h below          */
  68. #include "util.h"    /* Required by panel.h              */
  69. #include "panel.h"    /* For WPanel and current_panel          */
  70. #include "dialog.h"    /* For query_dialog()              */
  71. #include "main.h"    /* For cpanel, quit & init_sigchld()  */
  72. #include "global.h"    /* For home_dir                  */
  73. #include "cons.saver.h"    /* For handle_console(), etc.          */
  74. #include "key.h"    /* XCTRL and ALT macros              */
  75. #include "subshell.h"
  76.  
  77. /* Local functions */
  78. static int feed_subshell (int how, int fail_on_error);
  79. static void synchronize (void);
  80. static int pty_open_master (char *pty_name);
  81. static int pty_open_slave (const char *pty_name);
  82.  
  83. /* }}} */
  84. /* {{{ Definitions */
  85.  
  86. #ifndef STDIN_FILENO
  87. #    define STDIN_FILENO 0
  88. #endif
  89.  
  90. #ifndef STDOUT_FILENO
  91. #    define STDOUT_FILENO 1
  92. #endif
  93.  
  94. #ifndef STDERR_FILENO
  95. #    define STDERR_FILENO 2
  96. #endif
  97.  
  98. /* If using a subshell for evaluating commands this is true */
  99. int use_subshell =
  100. #ifdef SUBSHELL_OPTIONAL
  101. FALSE;
  102. #else
  103. TRUE;
  104. #endif
  105.  
  106. /* File descriptor of the pseudoterminal used by the subshell */
  107. int subshell_pty = 0;
  108.  
  109. /* If true, the child forked in init_subshell will wait in a loop to be attached by gdb */
  110. int debug_subshell = 0;
  111.  
  112. /* The key for switching back to MC from the subshell */
  113. char subshell_switch_key = XCTRL('o');
  114.  
  115. /* State of the subshell:
  116.  * INACTIVE: the default state; awaiting a command
  117.  * ACTIVE: remain in the shell until the user hits `subshell_switch_key'
  118.  * RUNNING_COMMAND: return to MC when the current command finishes */
  119. enum subshell_state_enum subshell_state;
  120.  
  121. /* Holds the latest prompt captured from the subshell */
  122. char *subshell_prompt = NULL;
  123.  
  124. /* Initial length of the buffer for the subshell's prompt */
  125. #define INITIAL_PROMPT_SIZE 10
  126.  
  127. /* Used by the child process to indicate failure to start the subshell */
  128. #define FORK_FAILURE 69  /* Arbitrary */
  129.  
  130. /* Initial length of the buffer for all I/O with the subshell */
  131. #define INITIAL_PTY_BUFFER_SIZE 100  /* Arbitrary; but keep it >= 80 */
  132.  
  133. /* For pipes */
  134. enum {READ=0, WRITE=1};
  135.  
  136.  
  137. /* Local variables */
  138.  
  139. static char *pty_buffer;    /* For reading/writing on the subshell's pty */
  140. static int pty_buffer_size;    /* The buffer grows as needed */
  141. static int subshell_pipe[2];    /* To pass CWD info from the subshell to MC */
  142. static pid_t subshell_pid;    /* The subshell's process ID */
  143. static char subshell_cwd[MC_MAXPATHLEN+1];  /* One extra char for final '\n' */
  144.  
  145. /* Subshell type (gleaned from the SHELL environment variable, if available) */
  146. static enum {BASH, TCSH, ZSH} subshell_type;
  147.  
  148. /* Flag to indicate whether the subshell is ready for next command */
  149. static int subshell_ready;
  150.  
  151. /* The following two flags can be changed by the SIGCHLD handler. This is */
  152. /* OK, because the `int' type is updated atomically on all known machines */
  153. static volatile int subshell_alive, subshell_stopped;
  154.  
  155. /* We store the terminal's initial mode here so that we can configure
  156.    the pty similarly, and also so we can restore the real terminal to
  157.    sanity if we have to exit abruptly */
  158. static struct termios shell_mode;
  159.  
  160. /* This counter indicates how many characters of prompt we have read */
  161. /* FIXME: try to figure out why this had to become global */
  162. static int prompt_pos;
  163.  
  164. /* }}} */
  165.  
  166. /* {{{ init_subshell */
  167.  
  168. /*
  169.  *  Fork the subshell, and set up many, many things.
  170.  *
  171.  *  Possibly modifies the global variables:
  172.  *    shell_mode
  173.  *    subshell_type, subshell_alive, subshell_stopped, subshell_pid
  174.  *    use_subshell - Is set to FALSE if we can't run the subshell
  175.  *    quit - Can be set to SUBSHELL_EXIT by the SIGCHLD handler
  176.  */
  177.  
  178. #ifdef HAVE_GRANTPT
  179. #    define SYNC_PTY_SIDES
  180. #else
  181. #    define SYNC_PTY_SIDES
  182. #endif
  183.  
  184. #undef SYNC_PTY_SIDES
  185.  
  186. #ifdef SYNC_PTY_SIDES
  187. /* Handler for SIGUSR1 (used below), does nothing but accept the signal */
  188. static void sigusr1_handler (int sig)
  189. {
  190. }
  191. #endif
  192.  
  193. void init_subshell (void)
  194. {
  195.     /* {{{ Local variables */
  196.  
  197.     /* This must be remembered across calls to init_subshell() */
  198.     static char pty_name[40];
  199.     int pty_slave;
  200.  
  201.     /* Braindead tcsh can't redirect output to a file descriptor? */
  202.     char tcsh_fifo[sizeof "/tmp/mc.pipe.1234567890"];
  203.  
  204.     
  205. #ifdef SYNC_PTY_SIDES
  206.     /* Used to wait for a SIGUSR1 signal from the subprocess */
  207.     sigset_t sigusr1_mask, old_mask;
  208. #endif
  209.  
  210.     /* }}} */
  211.  
  212.     if (subshell_pty == 0)  /* First time through */
  213.     {
  214.     /* {{{ Find out what type of shell we have */
  215.  
  216.     if (strstr (shell, "/zsh"))
  217.         subshell_type = ZSH;
  218.     else if (strstr (shell, "/tcsh"))
  219.         subshell_type = TCSH;
  220.     else if (strstr (shell, "/bash") || getenv ("BASH"))
  221.         subshell_type = BASH;
  222.     else
  223.     {
  224.         use_subshell = FALSE;
  225.         return;
  226.     }
  227.  
  228.     /* }}} */
  229.     /* {{{ Open a pty for talking to the subshell */
  230.  
  231.     /* FIXME: We may need to open a fresh pty each time on SVR4 */
  232.  
  233.     subshell_pty = pty_open_master (pty_name);
  234.     if (subshell_pty == -1)
  235.     {
  236.         fputs (__FILE__": couldn't open master side of pty\n", stderr);
  237.         perror ("pty_open_master");
  238.         use_subshell = FALSE;
  239.         return;
  240.     }
  241.     pty_slave = pty_open_slave (pty_name);
  242.     if (pty_slave == -1)
  243.     {
  244.         fprintf (stderr, "couldn't open slave side of pty (%s)\n\r",
  245.              pty_name);
  246.         use_subshell = FALSE;
  247.         return;
  248.     }
  249.  
  250.  
  251.     /* }}} */
  252.     /* {{{ Initialise the pty's I/O buffer */
  253.  
  254.     pty_buffer_size = INITIAL_PTY_BUFFER_SIZE;
  255.     pty_buffer = (char *) malloc (pty_buffer_size);
  256.  
  257.     /* }}} */
  258.     /* {{{ Create a pipe for receiving the subshell's CWD */
  259.  
  260.     if (subshell_type == TCSH)
  261.     {
  262.         sprintf (tcsh_fifo, "/tmp/mc.pipe.%d", getpid ());
  263.         if (mkfifo (tcsh_fifo, 0600) == -1)
  264.         {
  265.         perror (__FILE__": mkfifo");
  266.         use_subshell = FALSE;
  267.         return;
  268.         }
  269.  
  270.         /* Opening the FIFO as O_RDONLY or O_WRONLY causes deadlock */
  271.  
  272.         if ((subshell_pipe[READ] = open (tcsh_fifo, O_RDWR)) == -1 ||
  273.         (subshell_pipe[WRITE] = open (tcsh_fifo, O_RDWR)) == -1)
  274.         {
  275.         fprintf (stderr, "Couldn't open named pipe %s\n", tcsh_fifo);
  276.         perror (__FILE__": open");
  277.         use_subshell = FALSE;
  278.         return;
  279.         }
  280.     }
  281.     else  /* subshell_type is BASH or ZSH */
  282.         if (pipe (subshell_pipe))
  283.         {
  284.         perror (__FILE__": couldn't create pipe");
  285.         use_subshell = FALSE;
  286.         return;
  287.         }
  288.  
  289.     /* }}} */
  290.     }
  291.  
  292.     /* {{{ Define a handler for the sigusr1 signal */
  293.  
  294. #ifdef SYNC_PTY_SIDES
  295.     sigemptyset (&sigusr1_mask);
  296.     sigaddset (&sigusr1_mask, SIGUSR1);
  297.     sigprocmask (SIG_BLOCK, &sigusr1_mask, &old_mask);
  298.     signal (SIGUSR1, sigusr1_handler);
  299. #endif
  300.  
  301.     /* }}} */
  302.     /* {{{ Fork the subshell */
  303.  
  304.     subshell_alive = TRUE;
  305.     subshell_stopped = FALSE;
  306.     subshell_pid = fork ();
  307.     
  308.     if (subshell_pid == -1)
  309.     {
  310.     perror (__FILE__": couldn't spawn the subshell process");
  311.     /* We exit here because, if the process table is full, the */
  312.     /* other method of running user commands won't work either */
  313.     exit (1);
  314.     }
  315.  
  316.    /* }}} */
  317.  
  318.     if (subshell_pid == 0)  /* We are in the child process */
  319.     {
  320.     char *init_file = NULL;
  321.     
  322.     setsid ();  /* Get a fresh terminal session */
  323.  
  324.     /* {{{ Open the slave side of the pty: again */
  325.     pty_slave = pty_open_slave (pty_name);
  326.  
  327.     /* This must be done before closing the master side of the pty, */
  328.     /* or it will fail on certain idiotic systems, such as Solaris.    */
  329.  
  330.     /* Close master side of pty.  This is important; apart from    */
  331.     /* freeing up the descriptor for use in the subshell, it also    */
  332.     /* means that when MC exits, the subshell will get a SIGHUP and    */
  333.     /* exit too, because there will be no more descriptors pointing    */
  334.     /* at the master side of the pty and so it will disappear.    */
  335.  
  336.     close (subshell_pty);
  337.  
  338. #ifdef SYNC_PTY_SIDES
  339.         /* Give our parent process (MC) the go-ahead */
  340.         kill (getppid (), SIGUSR1);
  341. #endif
  342.  
  343.     /* }}} */
  344.     /* {{{ Make sure that it has become our controlling terminal */
  345.  
  346.     /* Redundant on Linux and probably most systems, but just in case: */
  347.  
  348. #    ifdef TIOCSCTTY
  349.     ioctl (pty_slave, TIOCSCTTY, 0);
  350. #    endif
  351.  
  352.     /* }}} */
  353.     /* {{{ Configure its terminal modes and window size */
  354.  
  355.     /* Set up the pty with the same termios flags as our own tty, plus  */
  356.     /* TOSTOP, which keeps background processes from writing to the pty */
  357.  
  358.     shell_mode.c_lflag |= TOSTOP;  /* So background writers get SIGTTOU */
  359.     if (tcsetattr (pty_slave, TCSANOW, &shell_mode))
  360.     {
  361.         perror (__FILE__": couldn't set pty terminal modes");
  362.         _exit (FORK_FAILURE);
  363.     }
  364.  
  365.     /* Set the pty's size (80x25 by default on Linux) according to the */
  366.     /* size of the real terminal as calculated by ncurses, if possible */
  367. #    if defined TIOCSWINSZ && !defined SCO_FLAVOR
  368.     {
  369.         struct winsize tty_size;
  370.         tty_size.ws_row = LINES;
  371.         tty_size.ws_col = COLS;
  372.         tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
  373.  
  374.         if (ioctl (pty_slave, TIOCSWINSZ, &tty_size))
  375.         perror (__FILE__": couldn't set pty size");
  376.     }
  377. #    endif
  378.  
  379.     /* }}} */
  380.     /* {{{ Set up the subshell's environment and init file name */
  381.  
  382.     /* It simplifies things to change to our home directory here, */
  383.     /* and the user's startup file may do a `cd' command anyway   */
  384.     chdir (home_dir);  /* FIXME? What about when we re-run the subshell? */
  385.  
  386.     switch (subshell_type)
  387.     {
  388.         case BASH:
  389.         init_file = ".mc.bashrc";
  390.         if (access (init_file, R_OK) == -1)
  391.             init_file += 3;   /* Skip the ".mc" part ;-) */
  392.  
  393.         /* Make MC's special commands not show up in bash's history */
  394.         putenv ("HISTCONTROL=ignorespace");
  395.  
  396.         /* Allow alternative readline settings for MC */
  397.         if (access (".mc.inputrc", R_OK) == 0)
  398.             putenv ("INPUTRC=.mc.inputrc");
  399.  
  400.         break;
  401.  
  402.         case TCSH:
  403.         init_file = ".mc.tcshrc";
  404.         if (access (init_file, R_OK) == -1)
  405.             init_file += 3;
  406.         break;
  407.  
  408.         case ZSH:
  409.         break;
  410.  
  411.         default:
  412.         fprintf (stderr, __FILE__": unimplemented subshell type %d\n",
  413.              subshell_type);
  414.         _exit (FORK_FAILURE);
  415.     }
  416.  
  417.     /* }}} */
  418.     /* {{{ Attach all our standard file descriptors to the pty */
  419.  
  420.     /* This is done just before the fork, because stderr must still     */
  421.     /* be connected to the real tty during the above error messages; */
  422.     /* otherwise the user will never see them.             */
  423.  
  424.     dup2 (pty_slave, STDIN_FILENO);
  425.     dup2 (pty_slave, STDOUT_FILENO);
  426.     dup2 (pty_slave, STDERR_FILENO);
  427.  
  428.     /* }}} */
  429.     /* {{{ Execute the subshell at last */
  430.  
  431.     close (subshell_pipe[READ]);
  432.     close (pty_slave);  /* These may be FD_CLOEXEC, but just in case... */
  433.  
  434.     switch (subshell_type)
  435.     {
  436.         case BASH:
  437.         execl (shell, "bash", "-rcfile", init_file, NULL);
  438.         break;
  439.  
  440.         case TCSH:
  441.         execl (shell, "tcsh", NULL);  /* What's the -rcfile equivalent? */
  442.         break;
  443.  
  444.         case ZSH:
  445.         execl (shell, "zsh", NULL);
  446.         break;
  447.     }
  448.  
  449.     /* If we get this far, everything failed miserably */
  450.     _exit (FORK_FAILURE);
  451.  
  452.     /* }}} */
  453.     }
  454.  
  455.     close(pty_slave);
  456.  
  457. #ifdef SYNC_PTY_SIDES
  458.     sigsuspend (&old_mask);
  459.     signal (SIGUSR1, SIG_DFL);
  460.     sigprocmask (SIG_SETMASK, &old_mask, NULL);
  461.     /* ...before installing our handler for SIGCHLD. */
  462. #endif
  463.  
  464. #if 0
  465.     /* {{{ Install our handler for SIGCHLD */
  466.  
  467.     init_sigchld ();
  468.  
  469.     /* We could have received the SIGCHLD signal for the subshell 
  470.      * before installing the init_sigchld */
  471.     pid = waitpid (subshell_pid, &status, WUNTRACED | WNOHANG);
  472.     if (pid == subshell_pid){
  473.     use_subshell = FALSE;
  474.     return;
  475.     }
  476.  
  477.     /* }}} */
  478. #endif
  479.  
  480.     /* {{{ Set up `precmd' or equivalent for reading the subshell's CWD */
  481.  
  482.     switch (subshell_type)
  483.     {
  484.     char precmd[80];
  485.  
  486.     case BASH:
  487.     sprintf (precmd, " PROMPT_COMMAND='pwd>&%d;kill -STOP $$'\n",
  488.          subshell_pipe[WRITE]);
  489.     goto write_it;
  490.  
  491.     case ZSH:
  492.     sprintf (precmd, "precmd(){ pwd>&%d;kill -STOP $$ }\n",
  493.          subshell_pipe[WRITE]);
  494.     goto write_it;
  495.  
  496.     case TCSH:
  497.     sprintf (precmd, "alias precmd 'echo $cwd>>%s;kill -STOP $$'\n", tcsh_fifo);
  498.  
  499.     write_it:
  500.     write (subshell_pty, precmd, strlen (precmd));
  501.     }
  502.  
  503.     /* }}} */
  504.     /* {{{ Wait until the subshell has started up and processed the command */
  505.  
  506.     subshell_state = RUNNING_COMMAND;
  507.     enable_interrupt_key ();
  508.     if (!feed_subshell (QUIETLY, TRUE)){
  509.     use_subshell = FALSE;
  510.     }
  511.     disable_interrupt_key ();
  512.     if (!subshell_alive)
  513.     use_subshell = FALSE;  /* Subshell died instantly, so don't use it */
  514.  
  515.     /* }}} */
  516. }
  517.  
  518. /* }}} */
  519. /* {{{ invoke_subshell */
  520.  
  521. int invoke_subshell (const char *command, int how, char **new_dir)
  522. {
  523.     /* {{{ Fiddle with terminal modes */
  524.  
  525.     static struct termios raw_mode = {0};
  526.     
  527.     /* MC calls reset_shell_mode() in pre_exec() to set the real tty to its */
  528.     /* original settings.  However, here we need to make this tty very raw, */
  529.     /* so that all keyboard signals, XON/XOFF, etc. will get through to the */
  530.     /* pty.  So, instead of changing the code for execute(), pre_exec(),    */
  531.     /* etc, we just set up the modes we need here, before each command.     */
  532.  
  533.     if (raw_mode.c_iflag == 0)  /* First time: initialise `raw_mode' */
  534.     {
  535.     tcgetattr (STDOUT_FILENO, &raw_mode);
  536.     raw_mode.c_lflag &= ~ICANON;  /* Disable line-editing chars, etc.   */
  537.     raw_mode.c_lflag &= ~ISIG;    /* Disable intr, quit & suspend chars */
  538.     raw_mode.c_lflag &= ~ECHO;    /* Disable input echoing            */
  539.     raw_mode.c_iflag &= ~IXON;    /* Pass ^S/^Q to subshell undisturbed */
  540.     raw_mode.c_iflag &= ~ICRNL;   /* Don't translate CRs into LFs        */
  541.     raw_mode.c_oflag &= ~OPOST;   /* Don't postprocess output        */
  542.     raw_mode.c_cc[VTIME] = 0;     /* IE: wait forever, and return as    */
  543.     raw_mode.c_cc[VMIN] = 1;      /* soon as a character is available   */
  544.     }
  545.  
  546.     tcsetattr (STDOUT_FILENO, TCSANOW, &raw_mode);
  547.  
  548.     /* }}} */
  549.     
  550.     /* Make the subshell change to MC's working directory */
  551.     if (new_dir)
  552.     do_subshell_chdir (cpanel->cwd, TRUE, 1);
  553.     
  554.     if (command == NULL)  /* The user has done "C-o" from MC */
  555.     {
  556.     if (subshell_state == INACTIVE)
  557.     {
  558.         subshell_state = ACTIVE;
  559.         /* FIXME: possibly take out this hack; the user can
  560.            re-play it by hitting C-hyphen a few times! */
  561.         write (subshell_pty, " \b", 2);  /* Hack to make prompt reappear */
  562.     }
  563.     }
  564.     else  /* MC has passed us a user command */
  565.     {
  566.     if (how == QUIETLY)
  567.         write (subshell_pty, " ", 1);
  568.     write (subshell_pty, command, strlen (command));
  569.     write (subshell_pty, "\n", 1);
  570.     subshell_state = RUNNING_COMMAND;
  571.     subshell_ready = FALSE;
  572.     }
  573.  
  574.     feed_subshell (how, FALSE);
  575.  
  576.     if (new_dir && subshell_alive && strcmp (subshell_cwd, cpanel->cwd))
  577.     *new_dir = subshell_cwd;  /* Make MC change to the subshell's CWD */
  578.  
  579.     /* Restart the subshell if it has died by SIGHUP, SIGQUIT, etc. */
  580.     while (!subshell_alive && !quit && use_subshell)
  581.     init_subshell ();
  582.  
  583.     prompt_pos = 0;
  584.  
  585.     return quit;
  586. }
  587.  
  588. /* }}} */
  589. /* {{{ read_subshell_prompt */
  590.  
  591. int read_subshell_prompt (int how)
  592. {
  593.     /* {{{ Local variables */
  594.  
  595.     int clear_now = FALSE;
  596.     static int prompt_size = INITIAL_PROMPT_SIZE;
  597.     int bytes = 0, i, rc = 0;
  598.     struct timeval timeleft = {0, 0};
  599.  
  600.     fd_set tmp;
  601.     FD_ZERO (&tmp);
  602.     FD_SET (subshell_pty, &tmp);
  603.  
  604.     /* }}} */
  605.  
  606.     if (subshell_prompt == NULL)  /* First time through */
  607.     {
  608.     subshell_prompt = (char *) malloc (prompt_size);
  609.     *subshell_prompt = '\0';
  610.     prompt_pos = 0;
  611.     }
  612.  
  613.     while (subshell_alive &&
  614.        (rc = select (FD_SETSIZE, &tmp, NULL, NULL, &timeleft)))
  615.     {
  616.     /* {{{ Check for `select' errors */
  617.  
  618.     if (rc == -1)
  619.         if (errno == EINTR)
  620.         continue;
  621.         else
  622.         {
  623.         tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
  624.         perror ("\n"__FILE__": select (FD_SETSIZE, &tmp...)");
  625.         exit (1);
  626.         }
  627.  
  628.     /* }}} */
  629.  
  630.     bytes = read (subshell_pty, pty_buffer, pty_buffer_size);
  631.     if (how == VISIBLY)
  632.         write (STDOUT_FILENO, pty_buffer, bytes);
  633.  
  634.     /* {{{ Extract the prompt from the shell output */
  635.  
  636.     for (i=0; i<bytes; ++i)
  637.         if (pty_buffer[i] == '\n' || pty_buffer[i] == '\r'){
  638.         prompt_pos = 0;
  639.         clear_now = FALSE;
  640.         } else {
  641.         clear_now = TRUE;
  642.         if (!pty_buffer [i])
  643.             continue;
  644.         
  645.         subshell_prompt[prompt_pos++] = pty_buffer[i];
  646.         if (prompt_pos == prompt_size)
  647.             subshell_prompt = (char *) realloc (subshell_prompt,
  648.                             prompt_size *= 2);
  649.         }
  650.  
  651.     /* Sometimes we get an empty new line and then nothing,
  652.      * we better just keep the old prompt instead. */
  653.     if (clear_now)
  654.         subshell_prompt[prompt_pos] = '\0';
  655.  
  656.     /* }}} */
  657.     }
  658.     if (rc == 0 && bytes == 0)
  659.     return FALSE;
  660.     return TRUE;
  661. }
  662.  
  663. /* }}} */
  664. /* {{{ resize_subshell */
  665.  
  666. void resize_subshell (void)
  667. {
  668. #if defined TIOCSWINSZ && !defined SCO_FLAVOR
  669.     struct winsize tty_size;
  670.  
  671.     tty_size.ws_row = LINES;
  672.     tty_size.ws_col = COLS;
  673.     tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
  674.  
  675.     if (ioctl (subshell_pty, TIOCSWINSZ, &tty_size))
  676.     perror (__FILE__": couldn't set pty size");
  677. #endif
  678. }
  679.  
  680. /* }}} */
  681. /* {{{ exit_subshell */
  682.  
  683. int exit_subshell (void)
  684. {
  685.     int quit = TRUE;
  686.     
  687.     if (subshell_state != INACTIVE && subshell_alive)
  688.     quit = !query_dialog (" Warning ", " The shell is still active."
  689.                   " Quit anyway? ", 0, 2, " Yes ", " No ");
  690.  
  691. #if AIX_TCSH_CODE_BELOW_IS_IT_FIXED
  692.     /* New Test code */
  693.     else
  694.     {
  695.     if (subshell_type == TCSH)
  696.         sprintf (pty_buffer, " echo -n Jobs:>/tmp/mc.pipe.%d;jobs>/tmp/"
  697.              "mc.pipe.%d;kill -STOP $$\n", getpid (), getpid ());
  698.     else
  699.         sprintf (pty_buffer, " echo -n Jobs:>&%d;jobs>&%d;kill -STOP $$\n",
  700.              subshell_pipe[WRITE], subshell_pipe[WRITE]);
  701.     write (subshell_pty, pty_buffer, strlen (pty_buffer));
  702.  
  703. #ifndef HAVE_GRANTPT  /* FIXME */
  704.     if (subshell_type == ZSH)
  705.         /* Here we have to drain the shell output, because zsh does a  */
  706.         /* tcsetattr(SHTTY, TCSADRAIN...) which will block if we don't */
  707.         read (subshell_pty, pty_buffer, pty_buffer_size);
  708. #endif
  709.  
  710.     /* TCSH + AIX hang here, fix this before removing the ifdef above */
  711.     if (read (subshell_pipe[READ], pty_buffer, pty_buffer_size) == 5)
  712.         quit = TRUE;
  713.     else
  714.         quit = !query_dialog (" Warning ", " There are stopped jobs."
  715.                   " Quit anyway? ", 0, 2, " Yes ", " No ");
  716.  
  717.     synchronize ();
  718.     subshell_state = RUNNING_COMMAND;
  719.     feed_subshell (QUIETLY, FALSE);  /* Drain the shell output (again) */
  720.     }
  721. #endif
  722.  
  723.     if (quit && subshell_type == TCSH)
  724.     {
  725.     /* We abuse of pty_buffer here, but it doesn't matter at this stage */
  726.     sprintf (pty_buffer, "/tmp/mc.pipe.%d", getpid ());
  727.     if (unlink (pty_buffer) == -1)
  728.         perror (__FILE__": couldn't remove named pipe /tmp/mc.pipe.NNN");
  729.     }
  730.     
  731.     return quit;
  732. }
  733.  
  734. /* }}} */
  735.  
  736. /* {{{ quote_directory */
  737. void quote_directory (int fd, char *string)
  738. {
  739.     int q;
  740.  
  741.     if (!string)
  742.     return;
  743.     
  744.     while (*string){
  745.     q = 0;
  746.     switch (*string){
  747.     case '"':  q = 1; break;
  748.     case '\\': q = 1; break;
  749.     case '\'': q = 1; break;
  750.     case ' ': q = 1; break;
  751.     }
  752.     if (q){
  753.         char q = '\\';
  754.  
  755.         write (fd, &q, 1);
  756.     }
  757.     write (fd, string, 1);
  758.     string++;
  759.     }
  760. }
  761.  
  762. /* }}} */
  763. /* {{{ do_subshell_chdir */
  764. /* If it actually changed the directory it returns true */
  765. void do_subshell_chdir (char *directory, int do_update, int reset_prompt)
  766. {
  767.     char *temp;
  768.     char *p;
  769.     
  770.     if (!(subshell_state == INACTIVE && strcmp (subshell_cwd, cpanel->cwd))){
  771.     /* We have to repaint the subshell prompt if we read it from
  772.      * the main program.  Please note that in the code after this
  773.      * if, the cd command that is sent will make the subshell
  774.      * repaint the prompt, so we don't have to paint it. */
  775.     if (do_update)
  776.         do_update_prompt ();
  777.     return;
  778.     }
  779.     
  780.     temp = strdup (directory);
  781.     
  782.     /* The initial space keeps this out of the command history (in bash) */
  783.     write (subshell_pty, " cd ", 4);
  784.     quote_directory (subshell_pty, temp);
  785.     write (subshell_pty, "\n", 1);
  786.     free (temp);
  787.     
  788.     subshell_state = RUNNING_COMMAND;
  789.     feed_subshell (QUIETLY, FALSE);
  790.     
  791.     if (subshell_alive && strcmp (subshell_cwd, cpanel->cwd))
  792.     fprintf (stderr, "\rWarning: couldn't change to %s.\n\r", cpanel->cwd);
  793.  
  794.     if (reset_prompt)
  795.     prompt_pos = 0;
  796.     update_prompt = FALSE;
  797.     /* Make sure that MC never stores the CWD in a silly format */
  798.     /* like /usr////lib/../bin, or the strcmp() above will fail */
  799. }
  800.  
  801. /* }}} */
  802. /* {{{ subshell_get_console_attributes */
  803.  
  804. void subshell_get_console_attributes (void)
  805. {
  806.     /* {{{ Get our current terminal modes */
  807.  
  808.     if (tcgetattr (STDOUT_FILENO, &shell_mode))
  809.     {
  810.     perror (__FILE__": couldn't get terminal settings");
  811.     use_subshell = FALSE;
  812.     return;
  813.     }
  814.  
  815.     /* }}} */
  816. }
  817.  
  818. /* }}} */
  819. /* {{{ sigchld_handler */
  820.  
  821. /* Figure out whether the subshell has stopped, exited or been killed */
  822. /* Possibly modifies: `subshell_alive', `subshell_stopped' and `quit' */
  823.  
  824. void sigchld_handler (int sig)
  825. {
  826.     int pid, status;
  827.  
  828.     pid = waitpid (subshell_pid, &status, WUNTRACED | WNOHANG);
  829.  
  830.     if (pid == subshell_pid) {
  831.     /* {{{ Figure out what has happened to the subshell */
  832.  
  833.     if (WIFSTOPPED (status))
  834.     {
  835.         if (WSTOPSIG (status) == SIGTSTP)
  836.         /* The user has suspended the subshell.  Revive it */
  837.         kill (subshell_pid, SIGCONT);
  838.         else
  839.         /* The subshell has received a SIGSTOP signal */
  840.         subshell_stopped = TRUE;
  841.     }
  842.     else  /* The subshell has either exited normally or been killed */
  843.     {
  844.         subshell_alive = FALSE;
  845.         if (WIFEXITED (status) && WEXITSTATUS (status) != FORK_FAILURE)
  846.         quit = SUBSHELL_EXIT;  /* Exited normally */
  847.     }
  848.  
  849.     /* }}} */
  850.     }
  851.  
  852.     pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
  853.     
  854.     if (pid == cons_saver_pid) {
  855.     /* {{{ Someone has stopped or killed cons.saver; restart it */
  856.  
  857.     if (WIFSTOPPED (status))
  858.         kill (pid, SIGCONT);
  859.     else
  860.     {
  861.         handle_console (CONSOLE_DONE);
  862.         handle_console (CONSOLE_INIT);
  863.         /* Ought to do: if (in_subshell) handle_console (CONSOLE_SAVE)
  864.            Can't do this without adding a new variable `in_subshell';
  865.            it hardly seems to be worth the trouble. */
  866.     }
  867.  
  868.     /* }}} */
  869.     }
  870.  
  871.     /* If we get here, some other child exited; ignore it */
  872. }
  873.  
  874. /* }}} */
  875.  
  876. /* {{{ feed_subshell */
  877.  
  878. /* Feed the subshell our keyboard input until it says it's finished */
  879.  
  880. static int feed_subshell (int how, int fail_on_error)
  881. {
  882.     /* {{{ Local variables */
  883.     fd_set read_set;    /* For `select' */
  884.     int bytes;        /* For the return value from `read' */
  885.     int i;        /* Loop counter */
  886.     int s;        /* return value from s */
  887.     
  888.     struct timeval wtime; /* Maximum time we wait for the subshell */
  889.     struct timeval *wptr;
  890.     /* }}} */
  891.  
  892.     /* we wait up to 10 seconds if fail_on_error */
  893.     wtime.tv_sec = 10;
  894.     wtime.tv_usec = 0;
  895.     
  896.     wptr = fail_on_error ? &wtime : NULL;
  897.     
  898.     for (;;)
  899.     {
  900.     /* {{{ Prepare the file-descriptor set and call `select' */
  901.  
  902.     FD_ZERO (&read_set);
  903.     FD_SET (subshell_pty, &read_set);
  904.     FD_SET (subshell_pipe[READ], &read_set);
  905.     if (how == VISIBLY)
  906.         FD_SET (STDIN_FILENO, &read_set);
  907.  
  908.     while (subshell_alive &&
  909.            (s = select (FD_SETSIZE, &read_set, NULL, NULL, wptr) == -1))
  910.         if (errno != EINTR)      /* Despite using SA_RESTART, we */
  911.         {              /* still have to check for this */
  912.         tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
  913.         perror ("\n"__FILE__": select (FD_SETSIZE, &read_set...)");
  914.         exit (1);
  915.         } else {
  916.         use_subshell = 0;
  917.         return FALSE;
  918.         }
  919.  
  920.     /* }}} */
  921.  
  922.     if (!subshell_alive)
  923.         return FALSE;
  924.  
  925.     /* From now on: block forever on the select call */
  926.     wptr = NULL;
  927.     
  928.     if (FD_ISSET (subshell_pty, &read_set))
  929.         /* {{{ Read from the subshell, write to stdout */
  930.  
  931.         /* This loop improves performance by reducing context switches
  932.                by a factor of 20 or so... unfortunately, it also hangs MC
  933.                randomly, because of an apparent Linux bug.  Investigate. */
  934.         /* for (i=0; i<5; ++i)  * FIXME -- experimental */
  935.         {
  936.         bytes = read (subshell_pty, pty_buffer, pty_buffer_size);
  937.         if (bytes == -1 && errno != EIO)
  938.         {
  939.             tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
  940.             perror ("\n"__FILE__": read (subshell_pty...)");
  941.             exit (1);
  942.         }
  943.         if (how == VISIBLY)
  944.             write (STDOUT_FILENO, pty_buffer, bytes);
  945.         }
  946.  
  947.         /* }}} */
  948.  
  949.     else if (FD_ISSET (subshell_pipe[READ], &read_set))
  950.         /* {{{ Read the subshell's CWD and capture its prompt */
  951.  
  952.         {
  953.         bytes = read (subshell_pipe[READ], subshell_cwd, MC_MAXPATHLEN+1);
  954.         if (bytes == -1)
  955.         {
  956.             tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
  957.             perror ("\n"__FILE__": read (subshell_pipe[READ]...)");
  958.             exit (1);
  959.         }
  960.         if (bytes >= 1)
  961.             subshell_cwd[bytes-1] = 0;  /* Squash the final '\n' */
  962.  
  963.         synchronize ();
  964.  
  965.         subshell_ready = TRUE;
  966.         if (subshell_state == RUNNING_COMMAND)
  967.         {
  968.             subshell_state = INACTIVE;
  969.             return 1;
  970.         }
  971.         }
  972.  
  973.         /* }}} */
  974.  
  975.     else if (FD_ISSET (STDIN_FILENO, &read_set))
  976.         /* {{{ Read from stdin, write to the subshell */
  977.  
  978.         {
  979.         bytes = read (STDIN_FILENO, pty_buffer, pty_buffer_size);
  980.         if (bytes == -1)
  981.         {
  982.             tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
  983.             perror ("\n"__FILE__": read (STDIN_FILENO, pty_buffer...)");
  984.             exit (1);
  985.         }
  986.  
  987.         for (i=0; i<bytes; ++i)
  988.             if (pty_buffer[i] == subshell_switch_key)
  989.             {
  990.             write (subshell_pty, pty_buffer, i);
  991.             if (subshell_ready)
  992.                 subshell_state = INACTIVE;
  993.             return TRUE;
  994.             }
  995.  
  996.         write (subshell_pty, pty_buffer, bytes);
  997.         subshell_ready = FALSE;
  998.         } else {
  999.         return FALSE;
  1000.         }
  1001.  
  1002.         /* }}} */
  1003.     }
  1004. }
  1005.  
  1006. /* }}} */
  1007. /* {{{ synchronize */
  1008.  
  1009. /* Wait until the subshell dies or stops.  If it stops, make it resume.  */
  1010. /* Possibly modifies the globals `subshell_alive' and `subshell_stopped' */
  1011.  
  1012. static void synchronize (void)
  1013. {
  1014.     sigset_t sigchld_mask, old_mask;
  1015.  
  1016.     sigemptyset (&sigchld_mask);
  1017.     sigaddset (&sigchld_mask, SIGCHLD);
  1018.     sigprocmask (SIG_BLOCK, &sigchld_mask, &old_mask);
  1019.  
  1020.     /* Wait until the subshell has stopped */
  1021.     while (subshell_alive && !subshell_stopped)
  1022.     sigsuspend (&old_mask);
  1023.     subshell_stopped = FALSE;
  1024.     kill (subshell_pid, SIGCONT);
  1025.  
  1026.     sigprocmask (SIG_SETMASK, &old_mask, NULL);
  1027.     /* We can't do any better without modifying the shell(s) */
  1028. }
  1029.  
  1030. /* }}} */
  1031. /* {{{ pty opening functions */
  1032.  
  1033. #ifdef SCO_FLAVOR
  1034.  
  1035. /* {{{ SCO version of pty_open_master */
  1036.  
  1037. static int pty_open_master (char *pty_name)
  1038. {
  1039.     int pty_master;
  1040.     int num;
  1041.     char *ptr;
  1042.  
  1043.     strcpy (pty_name, "/dev/ptyp");
  1044.     ptr = pty_name+9;
  1045.     for (num=0;;num++)
  1046.     {
  1047.     sprintf(ptr,"%d",num);    /* surpriiise ... SCO lacks itoa() */
  1048.     /* Try to open master */
  1049.     if ((pty_master = open (pty_name, O_RDWR)) == -1)
  1050.         if (errno == ENOENT)  /* Different from EIO */
  1051.         return -1;          /* Out of pty devices */
  1052.         else
  1053.         continue;          /* Try next pty device */
  1054.     pty_name [5] = 't';          /* Change "pty" to "tty" */
  1055.     if (access (pty_name, 6)){
  1056.         close (pty_master);
  1057.         pty_name [5] = 'p';
  1058.         continue;
  1059.     }
  1060.     return pty_master;
  1061.     }
  1062.     return -1;  /* Ran out of pty devices */
  1063. }
  1064.  
  1065. /* }}} */
  1066. /* {{{ SCO version of pty_open_slave */
  1067.  
  1068. static int pty_open_slave (const char *pty_name)
  1069. {
  1070.     int pty_slave;
  1071.     struct group *group_info = getgrnam ("terminal");
  1072.     
  1073.     if (group_info != NULL)
  1074.     {
  1075.     /* The following two calls will only succeed if we are root */
  1076.     /* [Commented out while permissions problem is investigated] */
  1077.     /* chown (pty_name, getuid (), group_info->gr_gid);  FIXME */
  1078.     /* chmod (pty_name, S_IRUSR | S_IWUSR | S_IWGRP);   FIXME */
  1079.     }
  1080.     if ((pty_slave = open (pty_name, O_RDWR)) == -1)
  1081.     perror ("open (pty_name, O_RDWR)");
  1082.     return pty_slave;
  1083. }
  1084.  
  1085. /* }}} */
  1086.  
  1087. #elif HAVE_GRANTPT
  1088.  
  1089. /* {{{ System V version of pty_open_master */
  1090.  
  1091. static int pty_open_master (char *pty_name)
  1092. {
  1093.     char *slave_name;
  1094.     int pty_master;
  1095.  
  1096.     strcpy (pty_name, "/dev/ptmx");
  1097.     if ((pty_master = open (pty_name, O_RDWR)) == -1
  1098.     || grantpt (pty_master) == -1          /* Grant access to slave */
  1099.     || unlockpt (pty_master) == -1          /* Clear slave's lock flag */
  1100.     || !(slave_name = ptsname (pty_master)))  /* Get slave's name */
  1101.     {
  1102.     close (pty_master);
  1103.     return -1;
  1104.     }
  1105.     strcpy (pty_name, slave_name);
  1106.     return pty_master;
  1107. }
  1108.  
  1109. /* }}} */
  1110. /* {{{ System V version of pty_open_slave */
  1111.  
  1112. static int pty_open_slave (const char *pty_name)
  1113. {
  1114.     int pty_slave = open (pty_name, O_RDWR);
  1115.  
  1116.     if (pty_slave == -1)
  1117.     {
  1118.     perror ("open (pty_name, O_RDWR)");
  1119.     return -1;
  1120.     }
  1121.  
  1122. #if !defined(__osf__)
  1123.     if (ioctl (pty_slave, I_PUSH, "ptem") == -1)
  1124.     {
  1125.     perror ("ioctl (pty_slave, I_PUSH, \"ptem\")");
  1126.     close (pty_slave);
  1127.     return -1;
  1128.     }
  1129.     
  1130.     if (ioctl (pty_slave, I_PUSH, "ldterm") == -1)
  1131.     {
  1132.     perror ("ioctl (pty_slave, I_PUSH, \"ldterm\")");
  1133.     close (pty_slave);
  1134.     return -1;
  1135.     }
  1136.  
  1137. #if !defined(sgi) && !defined(__sgi)
  1138.     if (ioctl (pty_slave, I_PUSH, "ttcompat") == -1)
  1139.     {
  1140.     perror ("ioctl (pty_slave, I_PUSH, \"ttcompat\")");
  1141.     close (pty_slave);
  1142.     return -1;
  1143.     }
  1144. #endif /* sgi || __sgi */
  1145. #endif /* __osf__ */
  1146.  
  1147.     return pty_slave;
  1148. }
  1149.  
  1150. /* }}} */
  1151.  
  1152. #else
  1153.  
  1154. /* {{{ BSD version of pty_open_master */
  1155.  
  1156. static int pty_open_master (char *pty_name)
  1157. {
  1158.     int pty_master;
  1159.     char *ptr1, *ptr2;
  1160.  
  1161.     strcpy (pty_name, "/dev/ptyXX");
  1162.     for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1; ++ptr1)
  1163.     {
  1164.     pty_name [8] = *ptr1;
  1165.     for (ptr2 = "0123456789abcdef"; *ptr2; ++ptr2)
  1166.     {
  1167.         pty_name [9] = *ptr2;
  1168.  
  1169.         /* Try to open master */
  1170.         if ((pty_master = open (pty_name, O_RDWR)) == -1)
  1171.         if (errno == ENOENT)  /* Different from EIO */
  1172.             return -1;          /* Out of pty devices */
  1173.         else
  1174.             continue;          /* Try next pty device */
  1175.         pty_name [5] = 't';          /* Change "pty" to "tty" */
  1176.         if (access (pty_name, 6)){
  1177.         close (pty_master);
  1178.         pty_name [5] = 'p';
  1179.         continue;
  1180.         }
  1181.         return pty_master;
  1182.     }
  1183.     }
  1184.     return -1;  /* Ran out of pty devices */
  1185. }
  1186.  
  1187. /* }}} */
  1188. /* {{{ BSD version of pty_open_slave */
  1189.  
  1190. static int pty_open_slave (const char *pty_name)
  1191. {
  1192.     int pty_slave;
  1193.     struct group *group_info = getgrnam ("tty");
  1194.  
  1195.     if (group_info != NULL)
  1196.     {
  1197.     /* The following two calls will only succeed if we are root */
  1198.     /* [Commented out while permissions problem is investigated] */
  1199.     /* chown (pty_name, getuid (), group_info->gr_gid);  FIXME */
  1200.     /* chmod (pty_name, S_IRUSR | S_IWUSR | S_IWGRP);   FIXME */
  1201.     }
  1202.     if ((pty_slave = open (pty_name, O_RDWR)) == -1)
  1203.     perror ("open (pty_name, O_RDWR)");
  1204.     return pty_slave;
  1205. }
  1206.  
  1207. /* }}} */
  1208.  
  1209. #endif
  1210.  
  1211. /* }}} */
  1212.  
  1213. #else
  1214. const int use_subshell = 0;
  1215. #endif /* HAVE_SUBSHELL_SUPPORT */
  1216.  
  1217. /* {{{ Emacs local variables */
  1218.  
  1219. /*
  1220.   Cause emacs to enter folding mode for this file:
  1221.   Local variables:
  1222.   folded-file: t
  1223.   end:
  1224. */
  1225.  
  1226. /* }}} */
  1227.